package com.hk.yajin.function.videos.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.GridLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.TypeReference;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hk.developmentlib.baseben.NewWorkBean;
import com.hk.developmentlib.callback.HttpCallBack;
import com.hk.developmentlib.utills.FastjsonUtils;
import com.hk.developmentlib.utills.UiUtil;
import com.hk.yajin.R;
import com.hk.yajin.base.SingleItemListFragment;
import com.hk.yajin.function.members.activity.LoginAndRegisterActivity;
import com.hk.yajin.function.videos.activity.VideoDetailActivity;
import com.hk.yajin.function.videos.adapter.VideoHeadViewAdapter;
import com.hk.yajin.function.videos.adapter.VideoQuickAdapter;
import com.hk.yajin.manager.UserInfoManager;
import com.hk.yajin.model.DataListModel;
import com.hk.yajin.model.LunBoData;
import com.hk.yajin.model.UserInfo;
import com.hk.yajin.model.Video;
import com.hk.yajin.utills.HttpRequestUtils;
import com.hk.yajin.utills.url.UrlConstants;
import com.jude.rollviewpager.OnItemClickListener;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.hintview.ColorPointHintView;

import org.xutils.common.util.LogUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 伦哥哥 on 2017/6/25.
 */

public class VideoListFragment extends SingleItemListFragment<Video> {
    private RollPagerView rollPagerView;
    private VideoHeadViewAdapter headViewAdapter;


    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container) {
        super.initView(inflater, container);
        //initHeadView();
        return mContentView;
    }


    @Override
    public void initAdapter() {
        mQuickAdapter = new VideoQuickAdapter(dataList, getActivity());
        mQuickAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
                Intent videoIntent = new Intent(getActivity(), VideoDetailActivity.class);
                videoIntent.putExtra(VIDEO, FastjsonUtils.toJSONString(dataList.get(position)));
                startActivity(videoIntent);
            }
        });
        super.initAdapter();
    }


    @Override
    public void setItemDecoration() {
        // mRecyclerView.addItemDecoration(new BaseItemBottomDecoration(getActivity(), 1));
    }

//    private void initHeadView() {
//        headview = getActivity().getLayoutInflater().inflate(R.layout.list_head_news, null, false);
//        rollPagerView = (RollPagerView) headview.findViewById(R.id.pv_lunbo);
//        rollPagerView.setHintView(new ColorPointHintView(getActivity(), Color.YELLOW, Color.WHITE));
//        rollPagerView.getLayoutParams().height = UiUtil.getScreenWidth() * 12 / 25;
//        headViewAdapter = new VideoHeadViewAdapter(headviewDatas, getActivity(), rollPagerView);
//        rollPagerView.setAdapter(headViewAdapter);
//        rollPagerView.setOnItemClickListener(new OnItemClickListener() {
//            @Override
//            public void onItemClick(int position) {
//                showShortToast("position");
//            }
//        });
//        mQuickAdapter.addHeaderView(headview);
//        headViewAdapter.notifyDataSetChanged();
//
//    }

    @Override
    protected void initRecyclerView() {
        super.initRecyclerView();
        final GridLayoutManager manager = new GridLayoutManager(mContext, 2, GridLayoutManager.VERTICAL, false);
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (position == mQuickAdapter.getItemCount() - 1) {
                    return manager.getSpanCount();
                } else {
                    return 1;
                }
            }
        });
        mRecyclerView.setLayoutManager(manager);

    }

    @Override
    public void initDataList() {
        if (loadFinish) {
            loadFinish = false;
            if (page == 1) {
                showLoadingView();
            }
            Map<String, Object> reqParam = UserInfoManager.getUserKeyMapIfLogin();
            reqParam.put("page", String.valueOf(page));
            reqParam.put("per_page", String.valueOf(pageSize));


            HttpRequestUtils.post(true, UrlConstants.FUNCTION_VIEW_VIDEO_LIST, reqParam, false, new HttpCallBack() {
                @Override
                public void onSucced(String data) {
                    hideLoadingView();
                    if (swipe_container != null && swipe_container.isRefreshing()) {
                        dataList.clear();
                    }
                    swipe_container.setRefreshing(false);
                    LogUtil.d("成功：" + data);
                    if (!TextUtils.isEmpty(data)) {
                        DataListModel<Video> model = FastjsonUtils.getBeanObject(data, new TypeReference<DataListModel<Video>>() {
                        });
                        handleData(model.getData());
                    } else {
                        mQuickAdapter.loadMoreFail();
                        mQuickAdapter.notifyDataSetChanged();
                        loadFinish = true;
                    }
                }

                @Override
                public void onBadHttp(String tip) {
                    hideLoadingView();
                    swipe_container.setRefreshing(false);
                    LogUtil.d("请求异常：" + tip);
                }

                @Override
                public void onErrorCode(NewWorkBean bean) {
                    hideLoadingView();
                    swipe_container.setRefreshing(false);
                    LogUtil.d("登录失败：" + bean.getMsg());
                }
            });
        }
    }

    @Override
    protected void initLunBo() {
//        Map<String, Object> reqParam = UserInfoManager.getUserKeyMapIfLogin();
//        reqParam.put("cl_id", String.valueOf("3"));
//        reqParam.put("limit", String.valueOf("3"));
//
//        HttpRequestUtils.post(false, UrlConstants.FUNCTION_LUN_BO, reqParam, false, new HttpCallBack() {
//            @Override
//            public void onSucced(String data) {
//                List<LunBoData> headDatas = FastjsonUtils.getBeanList(data, LunBoData.class);
//                if (headDatas != null && headDatas.size() > 0) {
//                    headviewDatas.clear();
//                    headviewDatas.addAll(headDatas);
//                    headViewAdapter.notifyDataSetChanged();
//                    headview.setVisibility(View.VISIBLE);
//                } else {
//                    headview.setVisibility(View.GONE);
//                }
//            }
//
//            @Override
//            public void onBadHttp(String tip) {
//
//            }
//
//            @Override
//            public void onErrorCode(NewWorkBean bean) {
//
//            }
//        });
    }

}
